package com.mocne.Common;

import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.util.ArrayList;
import java.util.Arrays;


public class RunPyScriptWithJava {
    public static void main(String[] args) {

        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("/Users/pengkaifeng/PycharmProjects/DarlingNeedHelp/Learn/xuanzhuanjuzhen.py");

        // 第一个参数为期望获得的函数（变量）的名字，第二个参数为期望返回的对象类型
        PyFunction pyFunction = interpreter.get("add", PyFunction.class);
        int a = 5, b = 10;
        //调用函数，如果函数需要参数，在Java中必须先将参数转化为对应的“Python类型”
        PyObject pyObj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
        System.out.println("the answer is: " + pyObj);


        // 第一个参数为期望获得的函数（变量）的名字，第二个参数为期望返回的对象类型
        PyFunction pyFunction1 = interpreter.get("xzjz", PyFunction.class);
        int[] a1 = {1, 2, 3};
        int[] b1 = {4, 5, 6};
        int[] c1 = {7, 8, 9};
        ArrayList obj = new ArrayList<>(Arrays.asList(a1, b1, c1));
        //调用函数，如果函数需要参数，在Java中必须先将参数转化为对应的“Python类型”
        PyObject pyObj1 = pyFunction1.__call__(new PyList(obj));
        System.out.println("the answer is: " + pyObj1);

    }
}
